package product_type

import (
	"context"
	"gitee.com/zyw0605688_admin/gozero/product_service/model"

	"gitee.com/zyw0605688_admin/gozero/product_service/internal/svc"
	"gitee.com/zyw0605688_admin/gozero/product_service/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductTypeCreateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductTypeCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductTypeCreateLogic {
	return &ProductTypeCreateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductTypeCreateLogic) ProductTypeCreate(req *types.ProductTypeCreateRequest) (resp *types.ProductTypeCreateResponse, err error) {
	productType := model.ProductType{
		ProductTypeName: req.ProductTypeName,
		ProductTypeCode: req.ProductTypeCode,
		ProductTypeDesc: req.ProductTypeDesc,
		SortCode:        req.SortCode,
	}
	err = l.svcCtx.DB.Save(&productType).Error
	if err != nil {
		return
	}
	resp = &types.ProductTypeCreateResponse{
		Id:              productType.Id,
		ProductTypeName: productType.ProductTypeName,
		ProductTypeCode: productType.ProductTypeCode,
		ProductTypeDesc: productType.ProductTypeDesc,
		SortCode:        productType.SortCode,
		CreatedAt:       productType.CreatedAt,
	}
	return
}
